package com.maila.xiaoaodispatch.net;

import android.content.Context;


import com.maila.xiaoaodispatch.utils.PrefUtil;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by mark on 2017/9/23.
 */
public class NetWork {

    private Context mContext;
    private VolleyInterface mResponseListener;
    String Tag = "NetWork";

    public NetWork(Context ctx, VolleyInterface listener) {
        mContext = ctx;
        mResponseListener = listener;
    }

    public NetWork(Context ctx, VolleyInterface listener, String tag) {
        this(ctx, listener);
        if (null != null) {
            this.Tag = tag;
        }
    }

    public void requestPost(String responseURL, String method) {
        requestPost(responseURL, method, null);
    }

    public void requestPost(String responseURL, String method, Map<String, Object> body) {
        requestQueryPost(responseURL, method, null, body);
    }

    /**
     * 上传单张图片
     * @param responseURL
     * @param method
     * @param body
     * @param filepartname
     * @param file
     */
    public void uploadOneImg(String responseURL, String method, Map<String, Object> body,String filepartname,File file) {
        requestUploadimg(responseURL, method, null, body,filepartname,file);
    }

    public void uploadMutiImg(String responseURL, String method, Map<String, Object> body,String filepartname,List<File> files) {
        requestUploadimg(responseURL, method, null, body,filepartname,files);
    }
    public void requestwxPost(String responseURL, Map<String, Object> body) {

    }

    public void requestQueryPost(String responseURL, String method, Map<String, Object> query, Map<String, Object> body) {
        String requestURL;
        if (null == method) {
            requestURL = getUrlString(responseURL, query);
        } else {
            requestURL = getUrlString(responseURL, method, query);
        }

        Map<String, String> params = getStringMap(body);
        VolleyRequest.RequestPost(mContext, requestURL, Tag, params, mResponseListener);

    }

    public  void requestUploadimg(String responseURL, String method, Map<String, Object> query,  Map<String, Object> body,String filsname, File files){
        String URL;
        if (null != method) {
            URL = getUrlString(responseURL, method, query);
        } else {
            URL = responseURL;
        }
        Map<String, String> params = getStringMap(body);
        VolleyRequest.uploadMultipart(mContext,URL,filsname,files,params,mResponseListener);
    }
public  void requestUploadimg(String responseURL, String method, Map<String, Object> query,  Map<String, Object> body,String filsname, List<File> files){
    String URL;
    if (null != method) {
        URL = getUrlString(responseURL, method, query);
    } else {
        URL = responseURL;
    }
    Map<String, String> params = getStringMap(body);
        VolleyRequest.uploadMultipart(mContext,URL,filsname,files,params,mResponseListener);
}
    private Map<String, String> getStringMap(Map<String, Object> body) {
        if (null == body) {
            return null;
        }
        Map<String, String> params = new HashMap<String, String>();
        for (String key : body.keySet()) {
            if (null != body.get(key)) {
                params.put(key, body.get(key).toString());
            }
        }
        return params;
    }

    public void requestGet(String responseURL, String method) {
        requestGet(responseURL, method, null);
    }


    public void requestGet(String responseURL, String method, Map<String, Object> query) {
        String URL;
        if (null != method) {
            URL = getUrlString(responseURL, method, query);
        } else {
            URL = responseURL;
        }
        VolleyRequest.RequestGet(mContext, URL, Tag, mResponseListener);
    }

    public String getUrlString(String responseURL, String method, Map<String, Object> body) {
        String qureyURL = responseURL + "&action=" + method + "&key=" + PrefUtil.getStringPref(mContext, "key", "") + "&device=" + PrefUtil.getStringPref(mContext, "InstallationId", "");
        String param = "";
        if (body != null) {
            for (String key : body.keySet()) {
                param = param + "&" + key + "=" + body.get(key);
            }
        }
        qureyURL = qureyURL + param;
        return qureyURL;
    }

    public String getUrlString(String responseURL, Map<String, Object> body) {
        String qureyURL = responseURL + "?";
        String param = "";
        if (body != null) {
            for (String key : body.keySet()) {
                param = param + "&" + key + "=" + body.get(key);
            }
        }
        qureyURL = qureyURL + param;
        return qureyURL;
    }

    private String getUrlString(String responseURL, String method) {
        return getUrlString(responseURL, method, null);
    }

    public void requestCancel(Context mContext, String tag) {
        if (null != tag) {
            VolleyRequest.CancelTag(mContext, tag);
        } else {
            VolleyRequest.CancelTag(mContext, Tag);
        }
    }

}
